#include "ex_SPL.h"
int Read_spl_file(record_def *mySPL, QString *message, QProgressBar *loading_file_progress_bar)
{
    QFile file(mySPL->filename);
    QStringList list, list2;
    QString mytext, text;
    QString line, temp, temp2, temp3;
    QVariant temp_variant;
    QString valid;
    QList<QStringList> VariablesList;
    QList<QStringList> VariablesList_values;
    QList<QStringList> data_1Freq;
    int i,j,k,l,n,count, nb_pin_value, nb_values, indxFreq ;
    int flag = 0;
    QStringList Variables;

    swp_rec _swp_rec;
    LP_rec _LP_rec;
    spl_rec _spl_rec;

    Variables_struc _Variables;
    Freq_and_Vars _Freq_and_Vars;

    Single_gamma_cmplx _Single_gamma_cmplx;
    List_gamma_cmplx _List_gamma_cmplx;
    //_Single_gamma_cmplx.val = new gsl_complex;

    mySPL->Shortfilename=QFileInfo(mySPL->filename).fileName();
    //mySPL->Gammas = Allocate2DArray< gsl_complex >(100, 10);
    //mySPL->Gammas = new gsl_complex[1000];
    //init GUI

    loading_file_progress_bar->setVisible( FALSE );


    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        //Add_message_to_status_windows();
        message->append("failed to open the file, please check the file");
        return 1;
    }

    QTextStream in(&file);
    indxFreq = 0;
    line = in.readLine();
    if( !line.contains("! Power sweep plan data file"))
    {
        //Add_message_to_status_windows("this file is not recognized as a Maury Swept Load V.4, please check the format");
        message->append( "this file is not recognized as a Maury Swept Load V.4, please check the format");
        return 1;
    }
    loading_file_progress_bar->setVisible( TRUE );
    loading_file_progress_bar->setEnabled( TRUE );
    mytext = "reading file, please wait !!";
    loading_file_progress_bar->setFormat(mytext);
    line = in.readLine();
    if( line.contains("!"))
    {
        line.remove("!");
        mySPL->Label.append(line);
    }
    line = in.readLine();
    if( line.contains("! Date/time"))
    {
        line.remove("!");
        mySPL->Date.append(line);
    }
    while (!line.isNull()) {
        line = in.readLine();
        if(line.contains("Active tuners:")){
            //Add_message_to_status_windows("this SPL seems to be Maury v.3, not supported in this current version");
            message->append( "this SPL seems to be Maury v.3, not supported in this current version");
            return 1;
        }
        if( line.contains("Number of Frequencies ="))
        {
            temp.clear();
            list = line.split(QRegExp("="));
            mySPL->nbFreq=QString(list[1]).toInt();
        }
        if( line.contains("Number of Variables = "))
        {
            temp.clear();
            list = line.split(QRegExp("="));
            mySPL->nbVariables=QString(list[1]).toInt();
        }
        if( line.contains("VAR=<"))
        {
            // read variables
            VariablesList.clear();
            Variables.clear();
            Variables << "freq" << "GHz" ;
            VariablesList.append(Variables);
            Variables.clear();
            for(i=0; i<mySPL->nbVariables;i++)
            {
                line.replace(QString("<"), QString("!"));
                line.replace(QString(">"), QString("!"));
                if(line.contains("Load") && line.contains("Gamma"))
                    mySPL->Sweep_Type="load_sweep";

                if(line.contains("Source") && line.contains("Gamma"))
                    mySPL->Sweep_Type="Source_sweep";

                list = line.split(QRegExp("!"));
                Variables << list[1] << list[3] ;

                line = in.readLine();
                VariablesList.append(Variables);
                Variables.clear();
            }

            // read ! Freq  Points per VAR
            for(i=0; i<mySPL->nbFreq;i++)
            {
                line = in.readLine();
                list = line.split(QRegExp(" "),  QString::SkipEmptyParts);
                VariablesList_values.append(list);
                _Freq_and_Vars.freqvar.clear();
                _Variables.Label=VariablesList.at(0).at(0);
                _Variables.Units=VariablesList.at(0).at(1);
                _Variables.var=QString(list.at(0)).toFloat();

                _Freq_and_Vars.freqvar.append(_Variables);

                for(j=1;j<list.count();j++){
                    _Variables.Label=VariablesList.at(j).at(0);
                    _Variables.Units=VariablesList.at(j).at(1);
                    _Variables.var=QString(list.at(j)).toInt();

                    _Freq_and_Vars.freqvar.append(_Variables);

                    _Freq_and_Vars.freq = QString(list.at(0)).toFloat();
                }
                mySPL->FreqVariables.append(_Freq_and_Vars);

                //_Freq_and_Vars
            }

        }
        _spl_rec.spl.clear();
        if( line.contains("Freq = "))
        {
            list = line.split(QRegExp(" "),  QString::SkipEmptyParts);
            if( mySPL->FreqVariables.at(indxFreq).freq == list[2].toFloat())
            {
                mytext.clear();
                QTextStream(&mytext) << "reading Frequency: " << mySPL->FreqVariables.at(indxFreq).freq;
                loading_file_progress_bar->setFormat(mytext);
                _spl_rec.Freq= mySPL->FreqVariables.at(indxFreq).freq;

                line = in.readLine(); // skip hamornics nb
                // get headers
                line = in.readLine();
                list  = line.split(QRegExp(" "),  QString::SkipEmptyParts);
                mySPL->Headers_list = list;

                data_1Freq.clear();
                //data_1Freq.append(list);
                // determine how many values for the Freq record: gammanb*pinnb*...
                //nb_values = mySPL->VariablesList_values.at(indxFreq).at(1).toInt();
                nb_values = mySPL->FreqVariables.at(indxFreq).freqvar.at(1).var;
                for(i=2;i< mySPL->FreqVariables.at(indxFreq).freqvar.count();i++)
                {
                    nb_values =nb_values *  mySPL->FreqVariables.at(indxFreq).freqvar.at(i).var;

                }

                // read values
                loading_file_progress_bar->setRange(0,nb_values);
                loading_file_progress_bar->setValue(0);
                count =0;
                flag = 0;
                //look for how many impedance at this freq
                j=0;
                temp = mySPL->FreqVariables.at(indxFreq).freqvar.at(j).Label;
                while(!temp.contains("Gamma")&& j < mySPL->FreqVariables.at(indxFreq).freqvar.count())
                {
                    j++;
                    temp = mySPL->FreqVariables.at(indxFreq).freqvar.at(j).Label;
                }
                nb_values = mySPL->FreqVariables.at(indxFreq).freqvar.at(j).var;
                //sweep across all impedance
                count =0;
                for(i=0;i<nb_values;i++)
                {
                    // look for how many Pin sweep at this impedance
                    j=0;

                    temp = mySPL->FreqVariables.at(indxFreq).freqvar.at(j).Label;
                    while(!temp.contains("Pin_avail"))
                    {
                        j++;
                        temp = mySPL->FreqVariables.at(indxFreq).freqvar.at(j).Label;
                    }
                    nb_pin_value = mySPL->FreqVariables.at(indxFreq).freqvar.at(j).var;
                    //sweep across Pin values
                    for(j=0;j<nb_pin_value;j++){
                        line = in.readLine();
                        list = line.split(QRegExp(" "),  QString::SkipEmptyParts);
                        valid = list.at(0);
                        if(valid.toInt()==1 )
                        {
                            data_1Freq.append(list);

                        }
                        count++;
                        loading_file_progress_bar->setValue(count);
                    }
                    _List_gamma_cmplx.List_Gamma.clear();
                    _List_gamma_cmplx.List_Impedance.clear();
                    _List_gamma_cmplx.List_Gamma_polar.clear();
                    //copy Impedances fundamental and harmonics
                    for(n=0;n<data_1Freq.at(0).count();n++){
                        temp = mySPL->Headers_list.at(n);
                        if (temp.contains("gamma_src") || temp.contains("gamma_ld")){
                            temp2 = data_1Freq.at(0).at(2*n-1);
                            GSL_SET_REAL(&_Single_gamma_cmplx.val,temp2.toFloat());
                            temp2 = data_1Freq.at(0).at(2*n);
                            GSL_SET_IMAG(&_Single_gamma_cmplx.val,temp2.toFloat());
                            temp.replace(QString("gamma"),QString("Grec"));
                            _Single_gamma_cmplx.Label = temp;
                            temp2.sprintf("%2.2f %2.2f ", GSL_REAL(_Single_gamma_cmplx.val), GSL_IMAG(_Single_gamma_cmplx.val));
                            temp = _Single_gamma_cmplx.Label +" " + temp2;
                             _List_gamma_cmplx.List_Gamma.append(_Single_gamma_cmplx);
                        }else if(temp.contains("Freq")) break;

                    }
                    //reformat in column for each parameter
                    for(k=(2*n-1);k<data_1Freq.at(0).count();k++){
                        _swp_rec.Label =mySPL->Headers_list.at(k-n+1);
                        for(l=0;l<data_1Freq.count();l++){
                            temp = data_1Freq.at(l).at(k);
                            _swp_rec.swp.append(temp.toFloat());
                        }
                        _swp_rec.compute_min_max();
                        _LP_rec.LP.append(new swp_rec(_swp_rec));
                        _swp_rec.swp.clear();
                    }
                    _List_gamma_cmplx.convert_gamma_2_impedance();
                    _List_gamma_cmplx.convert_gamma_2_polar();
                    _LP_rec.Add_compression_infos();
                    _LP_rec.compute_AMPM();

                    _LP_rec.gammas.append(_List_gamma_cmplx);
                    _LP_rec.valid=1;
                    _spl_rec.spl.append(new LP_rec(_LP_rec));

                    _LP_rec.LP.clear();
                    _LP_rec.gammas.clear();
                    _swp_rec.swp.clear();
                    data_1Freq.clear();
                    count++;

                }
            }

            _spl_rec.valid=1;
            mySPL->All_Freq_data.append(new spl_rec(_spl_rec));

            _LP_rec.LP.clear();
            data_1Freq.clear();
            indxFreq++;
        }
    }

    mytext = "File loaded in Memory, thanks";
    loading_file_progress_bar->setFormat(mytext);
    //mySPL->Add_compression_infos();
    //Add_message_to_status_windows(mySPL->filename + "...loaded");
    message->append(mySPL->filename + "...loaded");
    return 0;
}

int Read_swp_file(record_def *mySPL, QString *message, QProgressBar *loading_file_progress_bar)
{
    QFile file(mySPL->filename);
    QStringList list;
    QString mytext, text;
    QString line, temp, temp2;

    QList<QStringList> data_1Freq;
    int j,k,l,n,count ;
    QStringList Variables;

    qDebug(mySPL->filename.toAscii());
    swp_rec _swp_rec;
    LP_rec _LP_rec;
    spl_rec _spl_rec;
    Variables_struc _Variables;

    Freq_and_Vars _Freq_and_Vars;

    Single_gamma_cmplx _Single_gamma_cmplx;
    List_gamma_cmplx _List_gamma_cmplx;

    mySPL->Shortfilename=QFileInfo(mySPL->filename).fileName();


    loading_file_progress_bar->setVisible( FALSE );


    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        message->append("failed to open the file, please check the file");
        return 1;
    }

    QTextStream in(&file);
    line = in.readLine();
    if( !line.contains("! Swept power data file"))
    {
        message->append( "this file is not recognized as a Maury Swept power data file, please check the format");
        return 1;
    }
    loading_file_progress_bar->setVisible( TRUE );
    loading_file_progress_bar->setEnabled( TRUE );
    mytext = "reading file, please wait !!";
    loading_file_progress_bar->setFormat(mytext);
    line = in.readLine();
    if( line.contains("!"))
    {
        line.remove("!");
        mySPL->Label.append(line);
    }
    line = in.readLine();
    if( line.contains("! Date/time"))
    {
        line.remove("!");
        mySPL->Date.append(line);
    }
    while (!line.isNull()) {
        line = in.readLine();
        if( line.contains("Frequency"))
        {
            list = line.split(QRegExp(" "),  QString::SkipEmptyParts);
            _Freq_and_Vars.freq = QString(list[1]).toFloat();
            _Variables.Label="Pin_avail";
            _Variables.Units="dBm";
            _Variables.var=1;
            _Freq_and_Vars.freqvar.append(_Variables);
            mySPL->FreqVariables.append(_Freq_and_Vars);
        }


        _spl_rec.spl.clear();
        if( line.contains("Gamma_dut: "))
        {
            line.remove("Gamma_dut:");
            line.replace("="," ");
            line.replace("("," ");
            line.replace(")"," ");
            list = line.split(QRegExp(" "),  QString::SkipEmptyParts);

            _List_gamma_cmplx.List_Gamma.clear();
            _List_gamma_cmplx.List_Impedance.clear();
            _List_gamma_cmplx.List_Gamma_polar.clear();

            for(n=0;n< (list.count());n=n+3){
                 _Single_gamma_cmplx.Label = list[n];
                GSL_SET_REAL(&_Single_gamma_cmplx.val,list[n+1].toFloat());
                GSL_SET_IMAG(&_Single_gamma_cmplx.val,list[n+2].toFloat());
                _List_gamma_cmplx.List_Gamma.append(_Single_gamma_cmplx);
            }

            _List_gamma_cmplx.convert_gamma_2_impedance();
            _List_gamma_cmplx.convert_gamma_2_polar();

            line = in.readLine();
            line.remove("numpower_meas");
            line.remove("=");
            int numpower_meas = line.toInt();

            line = in.readLine();
            line.remove("!");
            list  = line.split(QRegExp(" "),  QString::SkipEmptyParts);
            mySPL->Headers_list = list;

            //sweep across Pin values
            loading_file_progress_bar->setRange(0,numpower_meas);
            loading_file_progress_bar->setValue(0);
            count = 0;
            for(j=0;j<numpower_meas;j++){
                line = in.readLine();
                list = line.split(QRegExp(" "),  QString::SkipEmptyParts);
                data_1Freq.append(list);
                count++;
                loading_file_progress_bar->setValue(count);
            }


            for(k=0;k<data_1Freq.at(0).count();k++){
                _swp_rec.Label =mySPL->Headers_list.at(k);
                for(l=0;l<data_1Freq.count();l++){
                    temp = data_1Freq.at(l).at(k);
                    _swp_rec.swp.append(temp.toFloat());
                }
                _swp_rec.compute_min_max();
                _LP_rec.LP.append(new swp_rec(_swp_rec));
                _swp_rec.swp.clear();
            }

            _LP_rec.Add_compression_infos();
            _LP_rec.compute_AMPM();
            //mySPL->All_Freq_data.at(i).spl.at(j)->LP.append(_LP_rec);
            _LP_rec.gammas.append(_List_gamma_cmplx);
            _spl_rec.spl.append(new LP_rec(_LP_rec));

            _LP_rec.LP.clear();
            _LP_rec.gammas.clear();

            _swp_rec.swp.clear();
            data_1Freq.clear();

            mySPL->All_Freq_data.append(new spl_rec(_spl_rec));

            _LP_rec.LP.clear();
            data_1Freq.clear();
    }
}//end while file ok

mytext = "File loaded in Memory, thanks";
loading_file_progress_bar->setFormat(mytext);

message->append(mySPL->filename + "...loaded");

return 0;
}




int Compute_optimums_at_X_on_parm(QProgressBar *progressBar_process,QString param, float val, int indx, Dataset *myDataset)
{
    QString temp, temp1, temp2;
    int i,j,k,m, pos_ref,pos_compression ;
    QList<float> X;
    QList<float> Yref, Y;
    QList<QString> result;
    int empty = 1, res=-1;
    float a,b,x1,x2,y1,y2, x,y, temp_float;
    swp_rec _swp_rec;
    LP_rec _LP_rec;
    spl_rec  _spl_rec;
    myDataset->record_optimums.All_Freq_data.clear();
    progressBar_process->setVisible( TRUE );
    progressBar_process->setEnabled( TRUE );
    QString mytext = "processing request...";
    progressBar_process->setFormat(mytext);

    //record_def _record_def;
    //temp = myDataset->record.at(0)->All_Freq_data.at(0)->spl.at(0)->LP.at(k)->Label;
    for(k=0;k<myDataset->record.at(indx)->All_Freq_data.count();k++)
    {
        empty =1;
        _spl_rec.spl.clear();
        progressBar_process->setRange(0,myDataset->record.at(indx)->All_Freq_data.at(k)->spl.count());
        for(j=0;j<myDataset->record.at(indx)->All_Freq_data.at(k)->spl.count();j++)
        {
            progressBar_process->setValue(j);

            X.clear();Y.clear();
            temp.sprintf("Compression");
            pos_compression = find_index_QListString(param.toAscii(), myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP);
            pos_ref = find_index_QListString("Pin_deliv_dBm", myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP);

            if(pos_compression!=-1 && pos_ref!=-1 ){
                Yref = myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP.at(pos_compression)->swp;
                X = myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP.at(pos_ref)->swp;
                empty = 1;
                if(Yref.first() < val && val < Yref.last())
                {
                    empty = 0;
                    i=0;
                    temp_float = Yref.at(0);
                    while(temp_float < val && i < X.count()){
                        temp_float = Yref.at(i);
                        i++;
                    }

                    if(temp_float >= val && Yref.at(1)< val){
                        i--;
                        //linear interpolation y=a*X+b
                        x1=X.at(i-1); y1 = Yref.at(i-1);
                        x2=X.at(i); y2 = Yref.at(i);
                        a= (y2-y1)/(x2-x1);
                        b = y1-a*x1;
                        x = (val-b)/a;
                        _LP_rec.LP.clear();
                        _LP_rec.gammas.clear();
                        for(m=0;m<myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP.count();m++)
                        {
                            Yref = myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP.at(m)->swp;
                            x1=X.at(i-1); y1 = Yref.at(i-1);
                            x2=X.at(i); y2 = Yref.at(i);
                            a= (y2-y1)/(x2-x1);
                            b = y1-a*x1;
                            y=a*x+b;
                            _swp_rec.Label.clear();
                            _swp_rec.swp.clear();
                            _swp_rec.Label =myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP.at(m)->Label;
                            _swp_rec.swp.append(y);
                            _swp_rec.IsEmpty = 0;
                            _LP_rec.valid=myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->valid;
                            _LP_rec.LP.append(new swp_rec(_swp_rec));
                            _LP_rec.gammas= myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->gammas;
                            _swp_rec.IsEmpty = 1;
                        }
                        _spl_rec.spl.append(new  LP_rec(_LP_rec));
                        temp.clear();
                        _spl_rec.valid = myDataset->record.at(indx)->All_Freq_data.at(k)->valid;
                        _spl_rec.Freq=myDataset->record.at(indx)->All_Freq_data.at(k)->Freq;

                    }//end if temp_float
                }//end if Yref
            }//end if pos_compression

            // pos_param = find_index_QListString(param.toAscii(), myDataset->record.at(indx)->All_Freq_data.at(k)->spl.at(j)->LP);

        }//end for Freq loop
        if(empty == 0)
        {
            myDataset->record_optimums.All_Freq_data.append(new spl_rec(_spl_rec));
            myDataset->record_optimums.Date = myDataset->record.at(indx)->Date;
            myDataset->record_optimums.filename = myDataset->record.at(indx)->filename;
            myDataset->record_optimums.filetype = myDataset->record.at(indx)->filetype;
            myDataset->record_optimums.Shortfilename = myDataset->record.at(indx)->Shortfilename;
            myDataset->record_optimums.Sweep_Type = myDataset->record.at(indx)->Sweep_Type;
            res = 0; // flag to show is there is any data whatever freq in the Record_optimums
        }
    }
    progressBar_process->setVisible( FALSE );
    progressBar_process->setEnabled( FALSE );
    return res;
}

QList<QString>  Find_Max_from_optimums(QProgressBar *progressBar_process, QList<QString> param, QList<QString> param2, Dataset *myDataset){
    int i,j,l, indx_max;
    int pos;
    float max;
    progressBar_process->setVisible( TRUE );
    progressBar_process->setEnabled( TRUE );
    QString mytext = "processing request...";
    progressBar_process->setFormat(mytext);
    QString temp, temp1, temp2;
    QList<QString> result;
    result.clear();
    temp.clear();
    for(l=0;l<param.count();l++)
    {
        temp.clear();
        QTextStream(&temp) <<"max for " << param.at(l);
        result.append(temp);

        for(i=0;i<myDataset->record_optimums.All_Freq_data.count();i++)
        {

            pos = find_index_QListString(param.at(l).toAscii(), myDataset->record_optimums.All_Freq_data.at(i)->spl.at(0)->LP);
            max = myDataset->record_optimums.All_Freq_data.at(i)->spl.at(0)->LP.at(pos)->swp.at(0);
            indx_max = 0;
            progressBar_process->setRange(0,myDataset->record_optimums.All_Freq_data.at(i)->spl.count());

            //find max
            for(j=0;j<myDataset->record_optimums.All_Freq_data.at(i)->spl.count();j++)
            {
                progressBar_process->setValue(j);
                if(myDataset->record_optimums.All_Freq_data.at(i)->spl.at(j)->valid == 1)
                {
                    pos = find_index_QListString(param.at(l).toAscii(), myDataset->record_optimums.All_Freq_data.at(i)->spl.at(j)->LP);
                    if( max < myDataset->record_optimums.All_Freq_data.at(i)->spl.at(j)->LP.at(pos)->swp.at(0)){
                        indx_max = j;
                        max = myDataset->record_optimums.All_Freq_data.at(i)->spl.at(j)->LP.at(pos)->swp.at(0);

                    }
                }//end if valid
            }
            temp.clear();
            temp2.clear();
            //copy impedances
            for(j=0;j<myDataset->record_optimums.All_Freq_data.at(i)->spl.at(indx_max)->gammas.at(0).List_Impedance.count();j++){
                temp.sprintf("= %2.2f + j* %2.2f\t", GSL_REAL(myDataset->record_optimums.All_Freq_data.at(i)->spl.at(indx_max)->gammas.at(0).List_Impedance.at(j).val),GSL_IMAG(myDataset->record_optimums.All_Freq_data.at(i)->spl.at(indx_max)->gammas.at(0).List_Impedance.at(j).val));
                QTextStream(&temp2) << myDataset->record_optimums.All_Freq_data.at(i)->spl.at(indx_max)->gammas.at(0).List_Impedance.at(j).Label << temp;
            }
            result.append(temp2);

            temp2.clear();
            temp1.clear();
            temp.clear();

            //compute all param selected for MAx
            QTextStream(&temp2) << "Freq"<<"\t";
            QTextStream(&temp1)<< myDataset->record_optimums.All_Freq_data.at(i)->Freq <<"\t";

            for(j=0;j<param2.count();j++)
            {
                //if(myDataset->record_optimums.All_Freq_data.at(i)->spl.at(j)->valid == 1)
                //{
                pos = find_index_QListString(param2.at(j).toAscii(), myDataset->record_optimums.All_Freq_data.at(i)->spl.at(indx_max)->LP);

                if( pos!=-1){
                    temp.sprintf("%2.2f\t",myDataset->record_optimums.All_Freq_data.at(i)->spl.at(indx_max)->LP.at(pos)->swp.at(0));
                    QTextStream(&temp2) << param2.at(j).toAscii()<<"\t";
                    QTextStream(&temp1) << temp;
                }
                //}// end if valid
            }

            result.append(temp2);
            QTextStream(&temp1) << " "<<endl;
            result.append(temp1);

        }// end Freq loop

        result.append(" ");
    }
    progressBar_process->setVisible( FALSE );
    progressBar_process->setEnabled( FALSE );
    return result;
}

int Read_lp_file(record_def *mySPL, QString *message, QProgressBar *loading_file_progress_bar)
{
    QFile file(mySPL->filename);
    QStringList list, list2;
    QString mytext;
    QString line, temp;
    QStringList QstringList_temp;
    QList<QStringList> data_1Freq;
    int indxFreq ;
    QStringList Variables;
    /*
    mySPL->Shortfilename=QFileInfo(mySPL->filename).fileName();
    //init GUI
    loading_file_progress_bar->setVisible( FALSE );


    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        message->append("failed to open the file, please check the file");
        return 1;
    }

    QTextStream in(&file);
    indxFreq = 0;
    line = in.readLine();
    if( !line.contains("! Load Pull Data file"))
    {
        message->append("this file is not recognized as a Maury Load Pull V.4, please check the format");
        return 1;
    }
    loading_file_progress_bar->setVisible( TRUE );
    loading_file_progress_bar->setEnabled( TRUE );
    mytext = "reading file, please wait !!";
    loading_file_progress_bar->setFormat(mytext);

    line = in.readLine();
    if( line.contains("!"))
    {
        line.remove("!");
        mySPL->Label.append(line);
    }
    line = in.readLine();
    if( line.contains("! Date/time"))
    {
        line.remove("!");
        mySPL->Date.append(line);
    }
    line = in.readLine();
    if( line.contains("Frequency"))
    {
        // line.remove("!");
        list  = line.split(QRegExp(" "));
        QstringList_temp.append(list.at(3));
        //mySPL->Date.append(list.at(3));
        mySPL->VariablesList_values.append(QstringList_temp);
        QstringList_temp.clear();
    }
    while (!line.isNull()) {
        line = in.readLine();
        if( line.contains("Static Gamma_dut: Source = ("))
        {
            line.remove("Static Gamma_dut: Source = (");
            line.remove(")");
            list  = line.split(QRegExp(" "));
            mySPL->Impedance.append(list);
            line = in.readLine();
            line.remove("!");
            list  = line.split(QRegExp(" "));
            data_1Freq.clear();
            data_1Freq.append(list);
            line = in.readLine();
        }
        if( line.contains("Gamma_dut:  "))
        {
            line.remove("Gamma_dut:  ");
            line.remove(")");
            list  = line.split(QRegExp(" "));
            data_1Freq << list;
            line = in.readLine();
            list  = line.split(QRegExp(" "));
            data_1Freq << list;
        }
        //   data_1Freq.clear();
    }
    //mySPL->data_nFreq.append(data_1Freq);

    mytext = "File loaded in Memory, thanks";
    loading_file_progress_bar->setFormat(mytext);
    message->append(mySPL->filename + "...loaded");
    */
    return 0;
}
